/**      
 * HttpLibs.java
 * Copyright (c) 2015-8-24 by penggq  
 * @author 彭国卿     
 * @version 1.0 
 * @Description 
 */
package com.ruoyi.express.third.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HttpLibs
 * @Description
 * @author 彭国卿
 * @Copyright 深圳市威富通科技有限公司
 * @version 1.0
 * @records {V版本号} {日期} {时间} {说明}<br>
 *          ==================================================<br>
 *          V1.0 2015-8-24 下午4:07:44 新创建 <br>
 */
public class HttpLibs {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpLibs.class);

    /**
     * 组装请求URL<br>
     * 按照key=value拼接方式构建<br>
     * 
     * @param params
     * @param actionURL
     * @return
     * @return String
     * @author 彭国卿 2015-1-5 下午3:43:57
     */
    public static String buildRequestURL(Map<String, Object> params, String actionURL) {
        return buildRequestURL(params, null, actionURL);
    }

    /**
     * 组装请求URL<br>
     * 按照格式化方式构建，如https://test.com/page?appid={0}&code={1}<br>
     * 
     * @author chenck
     * @date 2016年12月23日 下午8:12:36
     */
    public static String buildRequestURL(List<Object> params, String actionURL) {
        if (null != params && params.size() > 0) {
            return MessageFormat.format(actionURL, params.toArray());
        } else {
            return actionURL;
        }
    }

    /**
     * 组装请求URL
     * 
     * @param params
     *            参数Map
     * @param keyList
     *            拼装顺序,拼装的顺序<br>
     *            如果为空，则按默认顺序组装Map中的所有参数<br>
     *            如果不为空则按List中的顺序拼装
     * @param actionURL
     * @return String
     * @author:黄彦军
     * @date:2013-4-19
     * 
     */
    public static String buildRequestURL(Map<String, Object> params, List<String> keyList,
            String actionURL) {
        String paramsLink = buildParamsLink(params, keyList);
        return buildRequestURL(paramsLink, actionURL);
    }

    /**
     * 组装url
     * 
     * @param params
     * @param actionURL
     * @return
     * @return String
     * @author 彭国卿 2015-4-9 下午8:56:04
     */
    public static String buildRequestURL(String params, String actionURL) {
        if (StringUtils.isBlank(actionURL)) {
            return "";
        }
        if (StringUtils.isBlank(params)) {
            return actionURL;
        }

        StringBuffer url = new StringBuffer(actionURL);

        if (actionURL.indexOf(Libs.MARK_QUESTION) < 0) { // 不存在?号
            url.append(Libs.MARK_QUESTION);
        } else if (!Libs.MARK_QUESTION.equals(actionURL.charAt(actionURL.length() - 1))) {// 判断最后一个字符是不是
                                                                                          // ?
                                                                                          // 号
            url.append(Libs.MARK_AND);
        } else {
            url.append(Libs.MARK_QUESTION);
        }

        String res = url.append(params).toString();
        return res;
    }

    /**
     * 将URL返回的字符串解析成Map, 解析的字符串格式：a=a&b=b&c=c
     * 
     * @param urlParam
     * @return
     * @return Map<String,String>
     * @author 彭国卿 2015-1-5 下午4:20:33
     */
    public static Map<String, String> getUrlParamToMap(String urlParam) {
        if (StringUtils.isBlank(urlParam)) {
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
        try {
            String[] params = urlParam.split(Libs.MARK_AND);
            for (String param : params) {
                String[] values = param.split(Libs.MARK_EQUAL, 2);
                String value = values.length == 2 ? values[1] : "";
                map.put(values[0], URLDecoder.decode(value, Libs.UTF_8));
            }
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("参数拼接异常：", e);
        }
        return map;
    }

    public static List<NameValuePair> getParamsList(String urlParam) {
        if (StringUtils.isBlank(urlParam)) {
            return new ArrayList<NameValuePair>();
        }

        List<NameValuePair> res = new ArrayList<NameValuePair>();
        String[] params = urlParam.split(Libs.MARK_AND);
        for (String param : params) {
            String[] values = param.split(Libs.MARK_EQUAL, 2);
            String value = values.length == 2 ? values[1] : "";
            res.add(new BasicNameValuePair(values[0], value));
        }
        return res;
    }

    /**
     * 将传入的键/值对参数转换为NameValuePair参数集
     * 
     * @param paramsMap
     *            参数集, 键/值对
     * @return NameValuePair参数集
     */
    public static List<NameValuePair> getParamsList(Map<String, Object> paramsMap) {
        return getParamsList(paramsMap, null);
    }

    public static List<NameValuePair> getParamsList(Map<String, Object> paramsMap,
                                                    List<String> keyList) {
        if (paramsMap == null || paramsMap.size() == 0) {
            return null;
        }

        List<String> keys = null;
        if (keyList == null || keyList.size() == 0) {
            keys = new ArrayList<String>(paramsMap.keySet());
        } else {
            keys = keyList;
        }

        // 创建参数队列
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (String key : keys) {
            String value = paramsMap.get(key).toString();
            params.add(new BasicNameValuePair(key, value));
        }

        return params;
    }

    public static enum HttpClientMethod {
        GET, POST, PUT, DELETE
    }

    /**
     * Map转换为String请求串
     * 
     * @param paramMap
     * @return
     * @return String
     * @author 彭国卿 2015-1-5 下午3:42:11
     */
    public static String buildParamsLink(Map<String, Object> paramMap) {
        return buildParamsLink(paramMap, null);
    }

    /**
     * 组装URL
     * 
     * @param params
     *            参数Map
     * @param keyList
     *            拼装顺序,拼装的顺序, 如果为空，则按默认顺序组装Map中的所有参数, 如果不为空则按List中的顺序拼装
     * @param actionURL
     * @return String
     * @author:黄彦军
     * @date:2013-4-19
     * 
     */
    public static String buildParamsLink(Map<String, Object> params, List<String> keyList) {
        if (params == null || params.size() == 0) {
            return null;
        }

        List<String> keys = null;
        if (keyList == null || keyList.size() == 0) {
            keys = new ArrayList<String>(params.keySet());
        } else {
            keys = keyList;
        }

        List<String> res = new ArrayList<String>(keys.size());
        try {
            for (int i = 0; i < keys.size(); i++) {
                String key = keys.get(i);
                Object value = params.get(key);
                res.add(key + Libs.MARK_EQUAL + (value == null ? ""
                        : URLEncoder.encode(value.toString().trim(), Libs.UTF_8)));
            }
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("组装URL异常：", e);
        }
        String url = StringUtils.join(res, Libs.MARK_AND);
        return url;
    }

    /**
     * 将Map对象转换为k=v&k=v形式字符串<br>
     * 
     * @param params
     * @param removeBlankField
     *            true表示去掉为空和为null的字段
     * @return
     * @author chenck
     * @date 2017年3月8日 下午4:30:18
     */
    public static String map2Params(Map<String, Object> params, boolean removeBlankField) {
        StringBuilder sb = new StringBuilder(1024);
        boolean first = true;
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            // 移除值为空或为null的字段，不拼接到k=v串中
            if (removeBlankField
                    && (entry.getValue() == null || "".equals(entry.getValue().toString()))) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                sb.append("&");
            }
            sb.append(entry.getKey()).append("=").append(entry.getValue().toString());
        }
        return sb.toString();
    }
}
